package com.aloda.ojsandbox.handler;

import com.aloda.ojsandbox.exception.BaseException;
import com.aloda.ojsandbox.pojo.dto.ExecuteCodeResponse;
import com.aloda.ojsandbox.pojo.enums.ResponseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @Author aloda
 * @Date 2025/5/2 9:20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ExecuteCodeResponse exceptionHandler(BaseException ex){
        log.error("异常信息:", ex);
        return ExecuteCodeResponse.error(ex.getErrorCode(), ex.getMessage());
    }

    /**
     *  捕获其它异常
     * @param ex
     * @return
     */
    @ExceptionHandler
    public ExecuteCodeResponse otherExceptionHandler(RuntimeException ex) {
        log.error("异常信息:", ex);
        return ExecuteCodeResponse.error(ResponseEnum.FAILURE);
    }
}
